package com.music.service.playlists.dao.impl;

import java.util.Calendar;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.Transaction;

import com.music.service.jdosupport.PMF;
import com.music.service.playlists.beans.Playlist;
import com.music.service.playlists.constants.PlaylistState;
import com.music.service.playlists.dao.PlaylistDAO;
import com.music.service.songs.beans.Song;
import com.music.service.songs.beans.SongMemberUrl;
import com.music.service.songs.constants.SongState;
import com.music.service.urls.beans.Url;

public class PlaylistJDOImpl implements PlaylistDAO {

	public Playlist addPlaylist(Playlist pPlaylist) {
		pPlaylist.setPlaylistState(PlaylistState.ACTIVE);
		pPlaylist.setCreationDate(Calendar.getInstance().getTime());
		PersistenceManager persistenceManager = PMF.get()
				.getPersistenceManager();
		try {
			pPlaylist = persistenceManager.makePersistent(pPlaylist);
		} finally {
			persistenceManager.close();
		}
		return pPlaylist;
	}

	public void deleteSongFromPlaylist(Playlist pPlaylist, Song pSong) {
		if (pPlaylist == null || pSong == null) {
			throw new NullPointerException("Parameters are null ");
		}
		boolean isRemoved = pPlaylist.getPlaylistSongs().remove(
				pSong.getSongId());
		if (isRemoved == true) {
			PersistenceManager persistenceManager = PMF.get()
					.getPersistenceManager();
			try {
				pPlaylist = persistenceManager.makePersistent(pPlaylist);
			} finally {
				persistenceManager.close();
			}
		}

	}

	public void deletePlaylist(Playlist pPlaylist) {

		pPlaylist.setPlaylistState(PlaylistState.DELETED);
		pPlaylist.setLastAccessedDate(Calendar.getInstance().getTime());
		PersistenceManager persistenceManager = PMF.get()
				.getPersistenceManager();
		try {
			persistenceManager.makePersistent(pPlaylist);
		} finally {
			persistenceManager.close();
		}
	}

	public void addSongToPlaylist(Playlist pPlaylist, Song pSong) {
		if (pPlaylist == null || pSong == null) {
			throw new NullPointerException("Parameters are null ");
		}
		boolean isAdded = pPlaylist.getPlaylistSongs().add(pSong.getSongId());
		if (isAdded == true) {
			PersistenceManager persistenceManager = PMF.get()
					.getPersistenceManager();
			try {
				pPlaylist = persistenceManager.makePersistent(pPlaylist);
			} finally {
				persistenceManager.close();
			}
		}
	}

	public Song addSongToPlaylist(Playlist pPlaylist, Song pSong, Url pUrl) {
		if (pPlaylist == null || pSong == null || pUrl == null) {
			throw new NullPointerException("Parameters are null ");
		}

		PersistenceManager persistenceManager = PMF.get()
				.getPersistenceManager();
		Transaction currentTransaction = persistenceManager
				.currentTransaction();
		try {

			currentTransaction.begin();
			pSong.setSongState(SongState.ACTIVE);
			pSong = persistenceManager.makePersistent(pSong);

			pUrl.setSongId(pSong.getSongId());
			pUrl = persistenceManager.makePersistent(pUrl);

			SongMemberUrl songMemberUrl = new SongMemberUrl();
			songMemberUrl.setMemberId(pPlaylist.getMemberId());
			songMemberUrl.setSongId(pSong.getSongId());
			songMemberUrl.setUrlId(pUrl.getUrlId());
			songMemberUrl = persistenceManager.makePersistent(songMemberUrl);

			currentTransaction.commit();
		} finally {
			persistenceManager.close();
			if (currentTransaction.isActive()) {
				currentTransaction.rollback();
            }
		}
		return pSong;
	}

	public List<Playlist> viewPlaylists(Long pMemberId) {
		PersistenceManager persistenceManager = PMF.get()
				.getPersistenceManager();

		Query query = persistenceManager.newQuery(Playlist.class,
				"memberId == "+pMemberId+" && state == playlistStateParam");
		
		query.declareParameters("int playlistStateParam");
		List<Playlist> results;
		results = (List<Playlist>) query.execute(PlaylistState.ACTIVE.getState());

		return results;
	}

	public List<Playlist> getRecentPlaylists(Long pMemberId, int pSize) {
		PersistenceManager persistenceManager = PMF.get()
				.getPersistenceManager();

		Query query = persistenceManager.newQuery(Playlist.class,
				"memberId == "+pMemberId+" && state == playlistStateParam");
		
		query.declareParameters("int playlistStateParam");
		
		query.setRange(0, pSize);
		List<Playlist> results;
		results = (List<Playlist>) query.execute(PlaylistState.ACTIVE.getState());

		return results;
	}

	public Playlist getPlaylistInfoForMember(Long pMemberId, Long pPlaylistId) {
		Playlist playlist;
		PersistenceManager persistenceManager = PMF.get()
				.getPersistenceManager();
		try {
			playlist = persistenceManager.getObjectById(Playlist.class,
					pPlaylistId);
		} finally {
			persistenceManager.close();
		}

		return playlist;
	}

	public Playlist editPlaylist(Playlist pPlaylist) {
		pPlaylist.setLastUpdateDate(Calendar.getInstance().getTime());

		PersistenceManager persistenceManager = PMF.get()
				.getPersistenceManager();
		try {
			persistenceManager.makePersistent(pPlaylist);
		} finally {
			persistenceManager.close();
		}
		return pPlaylist;
	}

}
